package com.szq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.szq.common.R;
import com.szq.entity.Orders;
import com.szq.entity.User;
import com.szq.service.OrdersService;
import com.szq.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private UserService userService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime,String endTime){
        log.info("当前页数：{}.总页数：{}",page,pageSize);

        Page<Orders> pageInfo =new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(number!=null,Orders::getNumber,number)
                        .between(beginTime!=null||endTime!=null,Orders::getOrderTime,beginTime,endTime);

        ordersService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }


    @PutMapping
    public  R<String> status(@RequestBody Orders orders){
        log.info("订单：{}",orders);
        LambdaUpdateWrapper<Orders> queryWrapper=new LambdaUpdateWrapper<>();
        queryWrapper.eq(Orders::getId,orders.getId())
                .set(Orders::getStatus,orders.getStatus());

        ordersService.update(queryWrapper);
        return R.success("状态修改成功");
    }

    @GetMapping("userPage")
    public R<Page> page(int page,int pageSize){
        log.info("page:{},pageSize:{}",page,pageSize);
        Page<Orders> pageInfo=new Page<>(page,pageSize);

        ordersService.page(pageInfo);

        return R.success(pageInfo);
    }
}
